package com.grade.web.controller.system;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.grade.common.core.domain.entity.SysDictData;
import com.grade.common.core.domain.entity.SysUser;
import com.grade.common.utils.ShiroUtils;
import com.grade.system.service.ISysDictDataService;
import com.grade.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grade.common.annotation.Log;
import com.grade.common.enums.BusinessType;
import com.grade.system.domain.GradeImport;
import com.grade.system.service.IGradeImportService;
import com.grade.common.core.controller.BaseController;
import com.grade.common.core.domain.AjaxResult;
import com.grade.common.utils.poi.ExcelUtil;
import com.grade.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 已发布信用分导入Controller
 * 
 * @author grade
 * @date 2021-10-30
 */
@Controller
@RequestMapping("/system/gradeimport")
public class GradeImportController extends BaseController
{
    private String prefix = "system/gradeimport";

    @Autowired
    private IGradeImportService gradeImportService;
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:gradeimport:view")
    @GetMapping()
    public String gradeimport(ModelMap mmap)
    {
        SysUser currentUser = ShiroUtils.getSysUser();
        List<String>citys=new ArrayList<>() ;
        List<SysDictData>cityLabel=new ArrayList<>() ;
        if (currentUser != null&&!currentUser.isAdmin()){
            if(currentUser.getCity()!=null){
                citys = Arrays.asList(currentUser.getCity().split(","));
            }
            if(citys!=null && citys.size()>0){
                List<SysDictData> datas = dictTypeService.selectDictDataByType("b_city");
            //covert value to label

                for(SysDictData data: datas){
                    if(citys.contains(data.getDictValue())){
                        cityLabel.add(data);
                    }
                }
            }
            mmap.put("cityList",cityLabel);
        }else {
            List<SysDictData> datas = dictTypeService.selectDictDataByType("b_city");
            mmap.put("cityList",datas);
        }

        return prefix + "/gradeimport";
    }

    /**
     * 查询已发布信用分导入列表
     */
    @RequiresPermissions("system:gradeimport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GradeImport gradeImport)
    {
        //如果界面一个查询条件都没有，则查询国豪的
        //"gradeName" gradePeriod" company" "city"
//        if(isEmpty(gradeImport.getGradeName())&& isEmpty(gradeImport.getGradePeriod())
//            && isEmpty(gradeImport.getCompany()) && isEmpty(gradeImport.getCity())){
//            gradeImport.setCompany("国豪");
//        }

        if(isEmpty(gradeImport.getCompany())){
            gradeImport.setCompany("国豪");
        }

        SysUser currentUser = ShiroUtils.getSysUser();
        List<String>citys=new ArrayList<>() ;
        String[]cityLabel=new String[]{} ;
        if (currentUser != null&&!currentUser.isAdmin()){
            if(currentUser.getCity()!=null){
                citys = Arrays.asList(currentUser.getCity().split(","));
            }
            //covert value to label
            if(citys!=null && citys.size()>0){
                cityLabel=new String[citys.size()] ;
                List<SysDictData> datas = dictTypeService.selectDictDataByType("b_city");
                int i=0;
                for(SysDictData data: datas){
                    if(citys.contains(data.getDictValue())){
                        cityLabel[i++]=data.getDictLabel();
                    }
                }
            }
            gradeImport.setCitys(cityLabel);
        }
        startPage();
        List<GradeImport> list = gradeImportService.selectGradeImportList(gradeImport);
        return getDataTable(list);
    }

    /**
     * 新增已发布信用分导入
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GradeImport gradeImport = gradeImportService.selectGradeImportById(id);
        mmap.put("city", gradeImport.getCity());
        mmap.put("gradePeriod", gradeImport.getGradePeriod());
        mmap.put("gradeName", gradeImport.getGradeName());
        return prefix + "/detail";
    }

    @RequiresPermissions("system:gradeimport:list")
    @PostMapping("/listSelected")
    @ResponseBody
    public TableDataInfo listSelected(GradeImport gradeImport)
    {
        //此处使用order排序
        gradeImport.setUseOrder("asdf");

        SysUser currentUser = ShiroUtils.getSysUser();
        List<String>citys=new ArrayList<>() ;
        String[]cityLabel=new String[]{} ;
        if (currentUser != null&&!currentUser.isAdmin()){
            if(currentUser.getCity()!=null){
                citys = Arrays.asList(currentUser.getCity().split(","));
            }
            //covert value to label
            if(citys!=null && citys.size()>0){
                cityLabel=new String[citys.size()] ;
                List<SysDictData> datas = dictTypeService.selectDictDataByType("b_city");
                int i=0;
                for(SysDictData data: datas){
                    if(citys.contains(data.getDictValue())){
                        cityLabel[i++]=data.getDictLabel();
                    }
                }
            }
            gradeImport.setCitys(cityLabel);
        }
        List<GradeImport> list = gradeImportService.selectGradeImportList(gradeImport);
        return getDataTable(list);
    }

    private boolean isEmpty(String content){
        if(content==null || content.trim().length()==0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 导出已发布信用分导入列表
     */
    @RequiresPermissions("system:gradeimport:export")
    @Log(title = "已发布信用分导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GradeImport gradeImport)
    {
        List<GradeImport> list = gradeImportService.selectGradeImportList(gradeImport);
        ExcelUtil<GradeImport> util = new ExcelUtil<GradeImport>(GradeImport.class);
        return util.exportExcel(list, "已发布信用分导入数据");
    }

    /**
     * 新增已发布信用分导入
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //查询city并入列表
        List< SysDictData > dictData = dictTypeService.selectDictDataByType("b_city");
        mmap.put("dictData",dictData);
        return prefix + "/add";
    }

    /**
     * 新增保存已发布信用分导入
     */
    @RequiresPermissions("system:gradeimport:add")
    @Log(title = "已发布信用分导入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GradeImport gradeImport)
    {
        return toAjax(gradeImportService.insertGradeImport(gradeImport));
    }

    /**
     * 修改已发布信用分导入
     */
    @GetMapping("/edit/{Id}")
    public String edit(@PathVariable("Id") Long Id, ModelMap mmap)
    {
        GradeImport gradeImport = gradeImportService.selectGradeImportById(Id);
        mmap.put("gradeImport", gradeImport);
        return prefix + "/edit";
    }

    /**
     * 修改保存已发布信用分导入
     */
    @RequiresPermissions("system:gradeimport:edit")
    @Log(title = "已发布信用分导入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GradeImport gradeImport)
    {
        return toAjax(gradeImportService.updateGradeImport(gradeImport));
    }

    /**
     * 批量删除已发布信用分导入(删除某个地区的某个考核周期内的全部排名数据)
     */
    @RequiresPermissions("system:gradeimport:remove")
    @Log(title = "已发布信用分导入", businessType = BusinessType.DELETE)
    @PostMapping( "/batchRemove")
    @ResponseBody
    public AjaxResult batchRemove(String ids)
    {
        return toAjax(gradeImportService.deleteBatchGradeImportByIds(ids));
    }

    /**
     * 删除已发布信用分导入
     */
    @RequiresPermissions("system:gradeimport:remove")
    @Log(title = "已发布信用分导入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gradeImportService.deleteGradeImportByIds(ids));
    }



    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:gradeimport:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GradeImport> util = new ExcelUtil<GradeImport>(GradeImport.class);
        List<GradeImport> gradeImportList = util.importExcel(file.getInputStream());
        String message = gradeImportService.importGrade(gradeImportList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:gradeimport:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<GradeImport> util = new ExcelUtil<GradeImport>(GradeImport.class);
        return util.importTemplateExcel("评分导入模板");
    }

}
